package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.HotActivitiesList;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 11:26.
 */

public class HotActivitiesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_NO_DATA=1;
    private static final int TYPE_CONTENT=2;
    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    private List<HotActivitiesList.HotActivities> data;
    public HotActivitiesAdapter(List<HotActivitiesList.HotActivities> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==TYPE_NO_DATA){
            View itemView=inflater.inflate(R.layout.item_no_data,parent,false);
            return new NoDataHolder(itemView);
        }
        if(viewType==TYPE_CONTENT){
            View itemView=inflater.inflate(R.layout.item_hot_activities, parent, false);
            return new HotActivitiesHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof HotActivitiesHolder){
            HotActivitiesHolder activitiesHolder = (HotActivitiesHolder) holder;
            if(position==0){
                RecyclerView.MarginLayoutParams layoutParams=
                        (RecyclerView.MarginLayoutParams) holder.itemView.getLayoutParams();
                layoutParams.topMargin= DensityUtil.dp2px(15);
            }
            HotActivitiesList.HotActivities activities=data.get(position);
            activitiesHolder.tvTitle.setText(activities.getTitle());
            activitiesHolder.tvDateTime.setText(activities.getActTime());
            activitiesHolder.tvViewCount.setText(String.valueOf(activities.getActViewCount()));
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+activities.getImagemin(),
                    activitiesHolder.image,
                    R.mipmap.img_default_rectangle);
            activitiesHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (data == null||data.size()==0) ? 1 : data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (data==null||data.size()==0)?TYPE_NO_DATA:TYPE_CONTENT;
    }

    private class NoDataHolder extends RecyclerView.ViewHolder{
        private NoDataHolder(View itemView) {
            super(itemView);
            itemView.setVisibility(View.VISIBLE);
        }
    }

    private class HotActivitiesHolder extends RecyclerView.ViewHolder {
        private ImageView image;
        private TextView tvTitle;
        private TextView tvDateTime;
        private TextView tvViewCount;

        private HotActivitiesHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDateTime = (TextView) itemView.findViewById(R.id.tv_date_time);
            tvViewCount = (TextView) itemView.findViewById(R.id.tv_view_count);
        }
    }
}
